package com.ktwlsoft.fundmanage.fundFlow.processHandler.FundOutServiceTask;

import com.ktwlsoft.fundmanage.domain.entity.FundInRecord;
import com.ktwlsoft.fundmanage.domain.entity.TransferApply;
import com.ktwlsoft.fundmanage.fundFlow.processHandler.BaseServiceTask;
import com.ktwlsoft.fundmanage.payload.exceptions.BusinessError;
import com.ktwlsoft.fundmanage.payload.fundOut.FundOutOrCancelRequest;
import com.ktwlsoft.fundmanage.utils.KeyWorkerUtil;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PrepareRefund extends BaseServiceTask {

    @Override
    public void execute(DelegateExecution execution) {
        TransferApply transferApply = (TransferApply) execution.getVariable("transfer_prepare_refund");
        FundInRecord fundInRecord = fundOutService.getFundInRecordRepository()
                .findByDnCode(transferApply.getId()).get(0);
        if (fundInRecord == null) {
            throw new BusinessError("PrepareRefund: 未找到对应入账数据");
        }
        FundOutOrCancelRequest fundOutOrCancelRequest = new FundOutOrCancelRequest(transferApply.getId(),
                new Date(), transferApply.getTransferType().toString(), "退款划款单自动生成出账数据",
                String.valueOf(KeyWorkerUtil.nextId()), fundInRecord.getType());
        fundOutOrCancelRequest.setInstanceId(transferApply.getFlowInstanceId());
        execution.setVariable("fundOutOrCancelRequest", fundOutOrCancelRequest);
    }
}
